﻿using ControlFocusWin.Biz;
using ControlFocusWin.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ControlFocusWin.WinUtilApi
{
    /// <summary>
    /// Win32窗口API
    /// </summary>
    public class Win32WindowApi
    {

        #region 获取窗口信息

        /// <summary>
        /// 获取窗口标题信息
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="lpText">获取标题指针</param>
        /// <param name="nCount">存放标题空间长度</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpText, int nCount);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
          
        /// <summary>
        /// 获取父窗口
        /// </summary>
        /// <param name="hwnd">当前窗口句柄</param>
        /// <returns></returns>
        [DllImport("user32")]
        public static extern int GetParent(IntPtr hwnd);
         
        #endregion

        #region 设置窗口状态信息
         
        // 激活窗口
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);

        #endregion

        #region 查找窗口

        /// <summary>
        /// 遍历桌面应用顶级窗口
        /// </summary>
        /// <param name="callback">遍历窗口回调</param>
        /// <param name="param">应用参数</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern bool EnumWindows(CALLBACK callback, IntPtr param);

        /// <summary>
        /// 遍历桌面某一窗口下子级窗口，只遍历直属一级
        /// </summary>
        /// <param name="pHwnd">父窗口句柄</param>
        /// <param name="callback">遍历窗口回调</param>
        /// <param name="param">应用参数</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern bool EnumChildWindows(IntPtr pHwnd, CALLBACK callback, IntPtr param);

        /// <summary>
        /// 查找基于桌面的顶级窗口
        /// </summary>
        /// <param name="className"></param>
        /// <param name="windowName">窗口标题</param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string className, string windowName);
         
        /// <summary>
        /// 查找某个窗口中的子窗口
        /// </summary>
        /// <param name="hwndParent">父窗口</param>
        /// <param name="hwndChildAfter">第一个子窗口</param>
        /// <param name="lpszClass">窗口名</param>
        /// <param name="lpszWindow">窗口名</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
         
        /// <summary>
        /// 通过坐标获取可识别窗口句柄
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern IntPtr WindowFromPoint(PixelPoint pt);
         
        /// <summary>
        /// 获取当前窗口中，第一个子窗口
        /// </summary>
        /// <param name="hwnd">父窗口句柄</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern IntPtr GetTopWindow(IntPtr hwnd);



        [DllImport("user32.dll")]
        public static extern IntPtr ChildWindowFromPoint(IntPtr hwnd, PixelPoint pt);

        [DllImport("user32.dll", EntryPoint = "ChildWindowFromPoint")]
        public static extern IntPtr ChildWindowFromByCoor(IntPtr hwnd, int x, int y);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr ChildWindowFromPointEx(IntPtr hParent, PixelPoint pt, ChildFromPointFlags flags);

        #endregion
         
        /// <summary>
        /// 是否可显示
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool IsWindowVisible(IntPtr hWnd);
         
        /// <summary>
        /// 是否处于可用状态
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool IsWindowEnabled(IntPtr hWnd);


        /// <summary>
        /// 窗口显示设置
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="nCmdShow">窗口状态 nCmdShow ：0=关闭，1=正常，2=最小化，3=最大化</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int xCoor, int yCoor, int nWidth, int nHeight, bool bRepaint);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);


        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessageTextBox(IntPtr hWnd, int msg, int wParam, StringBuilder lParam);
    }
}
